import combineByKeyTestObject.combineByKeyTest
import org.apache.spark.{SparkConf, SparkContext}

/**
  * @author td
  * @date 2018/3/28
  */

object GroupByKeyTest {

  val list = List(("hello",1),("hello",1),("hello",1),("hello",1),("hello",1),("hello",1),("hello",1),("abc",1),("abc",1),("qwer",1),("qw",1));

  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("WordCount").setMaster("local");;
    val sc = new SparkContext(conf);
    groupByKeyTest(sc);

  }

  def groupByKeyTest(sparkContext: SparkContext): Unit = {

    val paraRdd = sparkContext.parallelize(list,2);

    val groupByRdd = paraRdd.groupByKey();
    val i = groupByRdd.map(item=>{
      (item._1,item._2.sum)
    });
    val r = i.collect()
    for (i<-r) {
      printf(i._1+", sum:"+i._2)
      }

    val result = groupByRdd.collect();
    for (item<- result) {
      println(item._1);
      val  intrator = item._2;
      intrator.foreach(println(_));
      println("----------------");

    }

  }

}
